<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title th:text="${con.getTitle()}"></title>
    <meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport">
    <link rel="stylesheet" th:href="@{//cdn.bootcss.com/twitter-bootstrap/3.4.1/css/bootstrap.min.css}">
    <link rel="shortcut icon" th:href="@{${con.getIco()}}" />
</head>
<body>
    <div class="modal fade" tabindex="-1" role="dialog">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title">信息</h4>
                </div>
                <div class="modal-body">
<!--                    <p>One fine body&hellip;</p>-->
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-primary" data-dismiss="modal">Close</button>
<!--                    <button type="button" class="btn btn-primary">Save changes</button>-->
                </div>
            </div><!-- /.modal-content -->
        </div><!-- /.modal-dialog -->
    </div><!-- /.modal -->

    <div class="container">
        <div th:if="${type} eq pj" class="panel panel-primary">
            <div class="panel-heading">青岛啤酒流量</div>
            <div class="panel-body">
                <form id="flow">
                    <div class="form-group">
                        <label for="flow_phone">手机号</label>
                        <input type="text" class="form-control" id="flow_phone" name="phone" placeholder="手机号">
                    </div>
                    <div class="form-group capchaInput" style="display: none">
                        <label for="flow_captcha">验证码</label>
                        <input type="text" class="form-control" id="flow_captcha" name="captcha" placeholder="验证码">
                    </div>
                    <input type="button" class="btn btn-primary getCaptcha" value="获取验证码">
                    <input type="hidden" class="btn btn-primary rush" value="冲鸭！">
                    <div style="margin-top: 20px" class="">
                        <p>注：青岛啤酒领流量，仅限联通号码使用，一天只能获取 3 次验证码(60秒内一次)，故一天最多只能领3次流量，一次30M，一月最多领10次(300M)。获取验证码之后如若没验证成功，可能将无法
                        获取验证码</p>
                        <p><a href="?type=cj">切换到抽奖挂机</a></p>
                    </div>
                </form>
            </div>
        </div>

        <div th:if="${type} eq cj" class="panel panel-primary">
            <div class="panel-heading">趣味天天摇（抽奖挂机）</div>
            <div class="panel-body">
                <form id="cron">
                    <div class="form-group">
                        <label for="cron_phone" class="control-label">手机号：</label>
                            <input type="text" class="form-control" id="cron_phone" name="phone" placeholder="手机号">
                    </div>
                    <div class="form-group">
                        <label for="code" class="control-label">验证码：</label>
                        <div class="input-group">
                            <input type="text" class="form-control" id="code" name="code" placeholder="验证码">
                            <span style="padding: 0;" class="input-group-addon">
                                <a href="javascript:refreshCode();"><img th:src="@{creatCode}" th:title="点击刷新验证码" id="codeImg"></a>
                            </span>
                        </div>
                    </div>
                    <input type="hidden" name="date">
                    <input type="button" class="btn btn-primary add" value="添加">
                    <input type="button" class="btn btn-primary query" value="查询中奖结果">
                    <input type="button" class="btn btn-primary delete" value="删除">
                    <div style="margin-top: 20px">
                        <p th:if="${con.getLotteryText()} ne ''" style="color: red" th:utext="${con.getLotteryText()}"></p>
                        <p>注：青岛啤酒(趣味天天摇)抽奖，仅限联通号码使用，一天抽奖3次，可能抽中50MB流量、100MB流量、1000MB流量、20钻石、幸运奖、15或50的开卡礼包（后2个都是没吊用的），
                            只要添加手机号码挂机，本系统将在凌晨（00:01）自动为你抽奖。系统会保存你的手机号码，介意请勿使用！已添加的可自行查询删掉。(经验：一个手机号码貌似一个月累计最多能抽中1000M
                            ，中不中流量看运气), 暂设置添加手机号最大数量为[[${max}]]</p>
                        <p><a href="?type=pj">切换到青岛啤酒流量</a></p>
                    </div>
                </form>
            </div>
        </div>
        <div th:utext="${con.getCopyright()}" class="text-center"></div>
    </div>
</body>
<script th:src="@{//cdn.bootcss.com/jquery/3.4.1/jquery.min.js}"></script>
<script th:src="@{//cdn.bootcss.com/twitter-bootstrap/3.4.1/js/bootstrap.min.js}"></script>
<script>
    let now = new Date();
    let date = now.getFullYear() + "-" + (now.getMonth() + 1) + "-" + now.getDate();
    let timestamp = getCookie("timestamp");
    $("input[name=date]").val(date);
    $(document).ready(function () {
        checkTime()
        $(".add").click(function () {
            let form = new FormData(document.getElementById("cron"));
            ajaxRequest("add", form, function (data) {
                refreshCode()
                if (data === -2){
                    //已存在号码
                    // showModal("<p>该号码已存在或者该号码不是联通号码或者添加号码达到最大限制</p>")
                    showModal("<p>输入的不是手机号码</p>")
                }else if (data === -1){
                    showModal("<p>添加号码数量已达最大限制</p>")
                }else if (data === 0) {
                    showModal("<p>该号码已存在</p>")
                }else if (data === -3){
                    showModal("<p>验证码不正确</p>")
                }else{
                    showModal("<p>添加成功！</p>");
                }
            })
        })
        $(".query").click(function () {
            let form = new FormData(document.getElementById("cron"));
            ajaxRequest("query", form, function (data) {
                if (data == ""){
                    showModal("<p>没有查询到任何中奖记录</p>")
                }else{
                    let ele = '<div class="table-responsive"><table class="table">';
                    ele += '<tr><th>日期</th><th>奖品</th></tr>';
                    for (i = 0; i < data.length; i++){
                        prize = data[i]
                        ele += '<tr><td>' + prize["date"] + '</td><td>' + prize["prize"] + '</td></tr>';
                    }
                    ele += '</table></div>';
                    showModal(ele)
                }
            })
        })
        $(".delete").click(function () {
            let form = new FormData(document.getElementById("cron"));
            ajaxRequest("delete", form, function (data) {
                showModal(data)
            })
        })
        $(".getCaptcha").click(function () {
            let form = new FormData(document.getElementById("flow"));
            ajaxRequest("get", form, function (data) {
                if (data === 1){
                    //ok
                    setCookie("timestamp", new Date().getTime())
                    $(this).attr("disabled", "disabled")
                    $(".capchaInput").show();
                    $(".getCaptcha").hide();
                    $(".rush").after("   <span style='color: red'>获取验证码成功</span>");
                    $(".rush").attr("type", "button");
                }else if (data === -1){
                    //
                    showModal("<p>获取验证码失败</p>")
                }else if (data === 0){
                    showModal("<p>该号码不是联通号码</p>")
                }
            })
        })
        $(".rush").click(function () {
            let form = new FormData(document.getElementById("flow"));
            ajaxRequest("verify", form, function (data) {
                if (data == false){
                    showModal("<p>验证码错误或者失效</p>")
                }else{
                    alert("领取成功")
                    location.reload()
                }
            })
        })
    });
    function clearModal() {
        $(".modal-body").html("");
    }
    function showModal(str) {
        clearModal();
        $(".modal-body").html(str);
        $(".modal").modal();
    }
    function ajaxRequest(url,form,func) {
        $.ajax({
            url: url,
            type: "POST",
            data: form,
            processData: false,
            contentType: false,
            success: function (data) {
                func(data)
            },
            error: function (data) {
                showModal("<p>网络链接异常或者是BUG触发了</p>")
            }
        })
    }
    function setCookie(name, value) {
        document.cookie = name + "="+ escape (value);
    }
    function getCookie(name) {
        var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
        if(arr=document.cookie.match(reg))
            return unescape(arr[2]);
        else
            return null;
    }
    function checkTime() {
        if (timestamp == null || parseInt((new Date().getTime() - timestamp) / 1000) > 60){
            return;
        }
        let interval;
        interval = setInterval(function () {
            let nowTime = new Date().getTime()
            let second = parseInt((nowTime - timestamp) / 1000)
            if (second < 60){
                $(".getCaptcha").attr("disabled", "disabled")
                $(".getCaptcha").val("还剩" + (60 - second) + "秒！")
            }else{
                $(".getCaptcha").removeAttr("disabled")
                $(".getCaptcha").val("获取验证码")
                clearInterval(interval)
            }
        }, 1000)
    }
    function refreshCode() {
        document.getElementById("codeImg").src="creatCode?" + new Date().getTime()
    }
</script>
</html>